package cc.comac.mom.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.mom.api.QualityQarVerifyResultService;
import cc.comac.mom.bean.QualityQarVerifyResultBean;
import cc.comac.mom.datahandlers.entity.QualityQarVerifyDataHandler;
import cc.comac.mom.entity.QualityQarVerifyEntity;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.util.TimeUtil;
import cc.comac.mom.util.soaputil.SoapCallUtil;
import cc.comac.mom.util.user.UserUtil;

@RestController
@Service
@Transactional
public class QualityQarVerifyResultServiceImpl implements QualityQarVerifyResultService {

	@Autowired
	private QualityQarVerifyDataHandler qualityQarVerifyDataHandler;
	
	@Autowired
	private UserUtil userUtil;
	/**
	 * 将bean中数据复制到Entity中
	 * **/
	public QualityQarVerifyEntity getEntityByBean(QualityQarVerifyResultBean qualityQarVerifyResultBean) {
		QualityQarVerifyEntity entity = new QualityQarVerifyEntity();
		entity.setCheckDate(qualityQarVerifyResultBean.getCheckDate());
		String checkUserJobNumber = qualityQarVerifyResultBean.getCheckUserJobNumber();
		entity.setCheckUser(userUtil.getUserTrueNameInfoByEcode(checkUserJobNumber));
		entity.setCheckUserJobNumber(checkUserJobNumber);
		entity.setReason(qualityQarVerifyResultBean.getReason());
		entity.setResult(qualityQarVerifyResultBean.getResult());
		entity.setId(qualityQarVerifyResultBean.getId());
		return entity;
	}
	@Override
	public ResultBean sendCorrectiveActionResult(@RequestBody QualityQarVerifyResultBean obj) {
		// TODO Auto-generated method stub
		ResultBean resultBean = new ResultBean(true);
		if (obj == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		// 更新bean中数据
		obj.setCheckUser(userUtil.getUserTrueNameInfoByEcode(obj.getCheckUserJobNumber()));
		obj.setCheckDate(TimeUtil.getLocalDate());
		
		// 更新纠正措施数据
		QualityQarVerifyEntity entityByBean = getEntityByBean(obj);
		updatearVerifyData(entityByBean);
		String json = obj.toString();
		Object[] results = null;
		try {
			Map<String, Object> invoke = SoapCallUtil.invoke("http://10.13.11.219/qmis/services/QMISSFSyncService?wsdl", "sendCorrectiveActionResult", json);
			if (invoke.size() >0) {
				 String status = invoke.get("status").toString();
				 String msg = invoke.get("msg").toString();
				 if(status != null && status.equals("1")) {
					 resultBean.setMessage(msg);
					 return resultBean; 
				 }else {
					 resultBean.setErrorMessge(msg);
					 return resultBean; 
				 }
			} else {
				resultBean.setErrorMessge("无法获取接口返回值");
				return resultBean;
			}
		
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			 resultBean.setErrorMessge(e.getMessage());
			 return resultBean; 
		}
		
	}

	/**接口17
	 * 查询纠正措施基本信息
	 */
	@Override
	public ResultBean getVerificationInfo(@RequestBody QualityQarVerifyResultBean obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		String json = obj.toString();
		Object[] results = null;
		try {
			Map<String, Object> invoke = SoapCallUtil.invoke("http://10.13.11.219/qmis/services/QMISSFSyncService?wsdl", "getVerificationInfo", json);
			if (invoke.size() > 0) {
				 String status = invoke.get("status").toString();
				 String msg = invoke.get("msg").toString();
				 if(status != null && status.equals("1")) {
					 resultBean.setObject(results[0].toString());
					 return resultBean; 
				 }else {
					 resultBean.setErrorMessge(msg);
					 return resultBean; 
				 }
			} else {
				resultBean.setErrorMessge("无法获取接口返回值");
				return resultBean;
			}
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			 resultBean.setErrorMessge(e.getMessage());
			 return resultBean; 
		}
		
	}

	@Override
	public ResultBean updatearVerifyData(@RequestBody QualityQarVerifyEntity qualityQarVerifyEntity) {
		ResultBean updateQarVerifyData = qualityQarVerifyDataHandler.updateQarVerifyData(qualityQarVerifyEntity);
		return updateQarVerifyData;
	}

}
